import 'package:flutter/material.dart';
import 'package:flutter_shopping/utils/colors.dart';
import 'package:flutter_shopping/utils/utils.dart';

class ShoppingAppBar extends StatelessWidget implements PreferredSizeWidget {
  final bool haveLeading;
  final Color? backgroundColor;
  final Color? backColor;
  final Color? searchColor;
  final Color? cartColor;

  const ShoppingAppBar({
    super.key,
    this.haveLeading = false,
    this.backgroundColor = Colors.white,
    this.backColor,
    this.searchColor,
    this.cartColor,
  });

  @override
  Widget build(BuildContext context) {
    return AppBar(
      backgroundColor: backgroundColor,
      surfaceTintColor: backgroundColor,
      leading: haveLeading
          ? IconButton(
              onPressed: () => Navigator.of(context).pop(),
              icon: Utils.loadSvg(
                'back',
                colorFilter: backColor != null ? ColorFilter.mode(backColor!, BlendMode.srcIn) : null,
              ),
            )
          : null,
      actions: [
        IconButton(
          onPressed: () => print('搜索'),
          icon: Utils.loadSvg(
            'search',
            // 由于此svg颜色默认是其他颜色，所以这里需要设置颜色
            colorFilter: searchColor != null
                ? ColorFilter.mode(
                    searchColor!,
                    BlendMode.srcIn,
                  )
                : const ColorFilter.mode(
                    kTextColor,
                    BlendMode.srcIn,
                  ),
          ),
        ),
        IconButton(
          onPressed: () => print('购物车'),
          icon: Utils.loadSvg(
            'cart',
            colorFilter: cartColor != null
                ? ColorFilter.mode(
                    cartColor!,
                    BlendMode.srcIn,
                  )
                : const ColorFilter.mode(
                    kTextColor,
                    BlendMode.srcIn,
                  ),
          ),
        ),
      ],
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight);
}
